package com.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.entity.PageResult;
import com.entity.QueryPageBean;
import com.entity.Result;
import com.pojo.CheckGroup;
import com.service.CheckGroupService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/CheckGroup")
public class CheckGroupController {
    @Reference
    private CheckGroupService checkGroupService;
    //<!--新增检查组-->
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @RequestMapping("/addCheckGroup")
    public Result addCheckGroup(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds){
        Result result = checkGroupService.addCheckGroup(checkGroup, checkitemIds);
        return result;
        //localhost:8080/CheckGroup/addCheckGroup.do?checkitemIds=87,89
    }
    @RequestMapping("/findAllCheckGroup")
    public PageResult findAllCheckGroup(QueryPageBean queryPageBean){
        PageResult allCheckGroup = checkGroupService.findAllCheckGroup(queryPageBean);
        return allCheckGroup;
    }
    //删除指定检查组,需要删除三张表中的数据
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @RequestMapping("/deleteCheckGroup")
    public Result deleteCheckGroup(int id){
        Result result = checkGroupService.deleteCheckGroup(id);
        return result;
    }
    //修改检查组,需要先把中间表相关数据干掉,再插入中间表数据
    @RequestMapping("/updateCheckGroup")
    public Result updateCheckGroup(@RequestBody CheckGroup checkGroup,@RequestParam("checkitemIds") Integer[] list) {
        Result result = null;
        try {
            result = checkGroupService.updateCheckGroup(checkGroup, list);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return result;
    }
    //根据gid查询所有有关的链表item数据
    @RequestMapping("/findItemByGid")
    public Result findItemByGid(@RequestParam("id") Integer gid){
        Result result= null;
        try {
            result = checkGroupService.findItemByGid(gid);
        } catch (Exception e) {
            e.printStackTrace();
            result=new Result(false,"数据查询失败");
            throw e;
        }
        return result;
    }
}
